package com.hu.lsp.Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author lsp
 * @Date 2020/8/25 13:32
 * @Version 1.0
 */
public class LockTest {

    public static void main(String[] args) {

        char[] a1 =  "12345".toCharArray();
        char[] aa="ABCDE".toCharArray();
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2=lock.newCondition();
        new Thread(()->{
               try {
                   lock.lock();
                   for (char a:a1) {
                       System.out.println(a);
                       condition2.signal();
                       condition1.await();

                   }
                   condition2.signal();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }finally {
                   lock.unlock();
               }



        },"t1").start();

        new Thread(()->{

               try {
                   lock.lock();
                   for(char a: aa) {
                       System.out.println(a);
                       condition1.signal();
                       condition2.await();
                   }
                   condition1.signal();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }finally {
                   lock.unlock();
               }

        },"t2").start();










    }

}

